package cn.dengta.webapp.user.entity.game;

import cn.dengta.webapp.base.entity.BaseEntity;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.Cmp;

/**
 * 游戏记录
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_game_log")
public class GameLog extends TimeBasedEntity {

	private static final long serialVersionUID = 1L;
	/** 游戏id */
	private Long gameId;
	/** 房间id 目前取用户id */
	private Long roomId;
	/** 房主id */
	private Long userId;
	/** 关联类型 */
	private String assocType;
	/** 关联目标 */
	private Long assocId;
	/** -9=已关闭; 0=已创建房间；1=正在进行中；9=已结束 */
	private Integer status;

	protected transient BaseEntity assocObj;

	public static final String GAME_ID = "game_id";
	public static final String STATUS = "status";
	public static final String ASSOC_TYPE = "assoc_type";
	public static final String ASSOC_ID = "assoc_id";

	public void handleAssocObject(BaseEntity obj) {
		this.assocObj = obj;
		this.assocType = obj.entityName();
		this.assocId = obj.getId();
	}

	/** 判断是否可用 */
	public boolean canStatus() {
		return Cmp.lt(this.status, 9) && Cmp.ge(this.status, 0);
	}

	/** 进行中 */
	public boolean canFor() {
		return Cmp.eq(this.status, 1);
	}

	/** 是否结束 */
	public boolean isEnd() {
		return Cmp.eq(this.status, 9) || Cmp.eq(this.status, -9);
	}

	public static GameLog of(Long gameId, Long roomId, Long userId) {
		GameLog log = new GameLog();
		log.setGameId(gameId);
		log.setRoomId(roomId);
		log.setUserId(userId);
		return log;
	}

}
